/**
 * 
 */
package com.bot.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bot.pojo.Applicant;
import com.bot.pojo.Job;
import com.bot.pojo.Score;
import com.bot.service.ApplicantService;
import com.github.pagehelper.PageInfo;

/**
 * @author zhaoyan
 *
 * 2020年2月3日
 */
@Controller
public class ApplicantController {
	
	
	@Autowired
	private ApplicantService applicantService;
	
	
	/**
	 * 	展示求职者面试成绩表
	 * @param pageNum
	 * @param m
	 * @param flag
	 * @return
	 */
	@RequestMapping("listApplicant")
	public String findApplicantByCon(
			@RequestParam(defaultValue = "1") Integer pageNum,
			Model m ,
			@RequestParam(defaultValue = "0")Integer flag ) {
		
		PageInfo<Applicant> applicantPage = applicantService.findApplicantByCon(pageNum,flag);
		
		
		
		m.addAttribute("applicantPage",applicantPage);
		
		return "list";
	}
	
	/**
	 * 	
	 * @return
	 */
	@RequestMapping("toAddApplicant")
	public String toAddApplicant(Model m) {
		
		Applicant applicant = new Applicant();
		
		m.addAttribute("applicant", applicant);
		
		return "addApplicant";
	}
	
	/**
	 * 接受应聘者的数据 保存到数据库
	 * @param request
	 * @return
	 */
	@RequestMapping("add")
	public String addApplicant(HttpServletRequest request,
			@ModelAttribute("applicant") @Valid Applicant applicant,BindingResult br) {
		//	学历和学位的约束关系
		
		//注意：如果学历选择了“专科”或“本科”，那么要通过程序验证学位只能是“学士”。
		if( (applicant.getEdu() != 3 ) 
			&& applicant.getDegree() != 1	) {
			br.rejectValue("degree", "degree", "学位是学士！");
		}
		//如果学历选择了“研究生”，那么要通过程序验证学位只能是“硕士”或“博士”
		if(applicant.getEdu() == 3 && (applicant.getDegree() == 1)) {
			br.rejectValue("degree", "degree", "学位是硕士或博士");
		}
		
		if(br.hasErrors()) {
			return "addApplicant";
		}
		
		applicantService.addApplicant(applicant);
		
		return "redirect:listApplicant";
	}
	
	
	
	@RequestMapping("toAddScore")
	public String toAddScore(Integer aid , Model m) {
		
		
		Applicant applicant = applicantService.findApplicantById(aid);
		
		List<Job> jobList = applicantService.findJobList();
		
		Score score = new Score();
		
		m.addAttribute("score", score);
		
		m.addAttribute("applicant", applicant);
		
		m.addAttribute("jobList",jobList);
		
		return "addScore";
		
	}
	
	
	/**
	 * 	添加
	 * @param request
	 * @param score
	 * @param result
	 * @return
	 */
	@RequestMapping("addScore")
	public String addScore(HttpServletRequest request,
			@ModelAttribute("score") @Valid Score score,BindingResult br) {
		
		
		//要按第3条实体设计要求，页面要演示以下验证效果：要验证应聘人的学历和学位要符合该职位最低学历和学位要求
		
		List<Job> findJobList = applicantService.findJobList();
		
		Applicant applicant = applicantService.findApplicantById(score.getAid());
		
		for (Job job : findJobList) {
			
			if(score.getJid() == job.getId()) {
				
				if( applicant.getEdu() < job.getRequire_edu() && applicant.getDegree() < job.getRequire_degree()) {
					
					br.rejectValue("jid", "jid", "应聘人的学历和学位不符合该职位最低学历和学位要求");
					break;
				}	
			}	
		}
		
		
		
		if(br.hasErrors()) {
			return "addScore";
		}
		
		
		if(score.getPerScore() > 100  || score.getPerScore() < 0) {
			
			br.rejectValue("perScore", "perScore", "打分范围必须在0-100之间");
		}
		
		
		if(br.hasErrors()) {
			return "addScore";
		}
		
		Integer num = applicantService.addScore(score);
		
		if(num > 0) {
			
			return "redirect:listApplicant";
			
		}
		
		return "addScore";
	}
	
	@RequestMapping("delApp")
	@ResponseBody
	public Integer delApp(Integer id) {
		
		return applicantService.delApp(id);	
		
	}
	
	
}
